package com.hykj.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hykj.dao.AboutUsDao;
import com.hykj.toolsbean.StringHandler;
import com.hykj.valuebean.aboutus;

/**
 * Servlet implementation class AboutUsServlet
 */
public class AboutUsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AboutUsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String servletpath = request.getServletPath();
		if ("/aboutus/getAllAboutUs".equals(servletpath)) {
			getAllAboutUs(request, response);
		} else if ("/admin/aboutus/insertAboutUs".equals(servletpath)) {
			insertAboutUs(request, response);
		} else if ("/aboutus/viewAboutUs".equals(servletpath)) {
			viewAboutUs(request, response);
		} else if ("/admin/listAboutUs".equals(servletpath)) {
			listAboutUs(request, response);
		} else if ("/admin/aboutus/viewAboutUs".equals(servletpath)) {
			adminViewAboutUs(request, response);
		} else if ("/admin/aboutus/deleteAboutUs".equals(servletpath)) {
			deleteAboutUs(request, response);
		} else if ("/aboutus/getCategoryAboutUs".equals(servletpath)) {
			getCategoryAboutUs(request, response);
		} else if ("/admin/aboutus/updateAboutUs".equals(servletpath)) {
			updateAboutUs(request, response);
		} else if ("/admin/aboutus/getCategoryAboutUs".equals(servletpath)) {
			adminGetCategoryAboutUs(request, response);
		}
	}
		
	protected void adminGetCategoryAboutUs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			AboutUsDao AboutUsDaoIns = new AboutUsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/admin/aboutus/getCategoryAboutus?category=" + category;
			List<?> AboutUsList = AboutUsDaoIns.getCategoryAboutUs(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("NewsList", AboutUsList);
			request.setAttribute("pageBar", AboutUsDaoIns.getDaoPage());
			AboutUsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/admin/aboutus/listAboutUs.jsp");
		rd.forward(request, response);
	}

	protected void getAllAboutUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			AboutUsDao AboutUsDaoIns = new AboutUsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/aboutus/getAllAboutus";
			List<?> AboutUsList = AboutUsDaoIns.getAllAboutUs(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("AboutUsList", AboutUsList);
			request.setAttribute("pageBar", AboutUsDaoIns.getDaoPage());
			AboutUsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/aboutus/aboutus.jsp");
		rd.forward(request, response);
	}
	
	protected void updateAboutUs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AboutUsDao AboutUsDaoIns = new AboutUsDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		aboutus aboutusIns = new aboutus();
		aboutusIns.setArticle(request.getParameter("article"));
		aboutusIns.setAuthor(request.getParameter("author"));
		aboutusIns.setDate(dateformat.format(date));
		aboutusIns.setOriginal(request.getParameter("original"));
		aboutusIns.setTitle(request.getParameter("title"));
		aboutusIns.setCategory(request.getParameter("category"));
		aboutusIns.setId(StringHandler.strToint(request.getParameter("id")));
		
		AboutUsDaoIns.updateAboutUs(aboutusIns);
		AboutUsDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listAboutUs");
		rd.forward(request, response);
	}

	protected void insertAboutUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		AboutUsDao aboutusDaoIns = new AboutUsDao();
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd kk:mm");
		Date date = new Date();
		String title = request.getParameter("title");
		String author = request.getParameter("author");
		String original = request.getParameter("original");
		String article = request.getParameter("article");
		String category = request.getParameter("category");

		aboutus aboutusIns = new aboutus();
		aboutusIns.setArticle(article);
		aboutusIns.setAuthor(author);
		aboutusIns.setDate(dateformat.format(date));
		aboutusIns.setOriginal(original);
		aboutusIns.setTitle(title);
		aboutusIns.setCategory(category);

		aboutusDaoIns.insertAboutUs(aboutusIns);
		aboutusDaoIns.closed();

		RequestDispatcher rd = request.getRequestDispatcher("/admin/listAboutUs");
		rd.forward(request, response);
	}

	protected void viewAboutUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			AboutUsDao aboutusDaoIns = new AboutUsDao();
			aboutus aboutusIns = aboutusDaoIns.getSingleAboutUs(newId);
			aboutusDaoIns.closed();

			if (aboutusIns != null) {
				request.setAttribute("newsIns", aboutusIns);
			}

		}
		RequestDispatcher rd = request.getRequestDispatcher("/aboutus/viewAboutUs.jsp");
		rd.forward(request, response);
	}

	protected void listAboutUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			AboutUsDao aboutusDaoIns = new AboutUsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String goWhich = "/hykj/admin/listAboutUs";
			List<?> NewsList = aboutusDaoIns.getAllAboutUs(strcurrentP, strcurrentG,
					goWhich);
			request.setAttribute("NewsList", NewsList);
			request.setAttribute("pageBar", aboutusDaoIns.getDaoPage());
			aboutusDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/aboutus/listAboutUs.jsp");
		rd.forward(request, response);
	}

	protected void adminViewAboutUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Integer newId = StringHandler.strToint(request.getParameter("newId"));
		if (newId != null) {

			AboutUsDao aboutusDaoIns = new AboutUsDao();
			aboutus aboutusIns = aboutusDaoIns.getSingleAboutUs(newId);
			aboutusDaoIns.closed();

			if (aboutusIns != null) {
				request.setAttribute("aboutusIns", aboutusIns);
			}

		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/admin/aboutus/updateAboutUs.jsp");
		rd.forward(request, response);
	}

	protected void deleteAboutUs(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String newId = request.getParameter("newId");
		AboutUsDao AboutUsDaoIns = new AboutUsDao();
		AboutUsDaoIns.deleteAboutUs(newId);
		AboutUsDaoIns.closed();
		
		RequestDispatcher rd = request.getRequestDispatcher("/admin/listAboutUs");
		rd.forward(request, response);

	}
	
	protected void getCategoryAboutUs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			AboutUsDao AboutUsDaoIns = new AboutUsDao();
			String strcurrentP = request.getParameter("currentP");
			String strcurrentG = request.getParameter("currentG");
			String category = request.getParameter("category");
			String goWhich = "/hykj/aboutus/getCategoryAboutus" + category;
			List<?> AboutUsList = AboutUsDaoIns.getCategoryAboutUs(strcurrentP, strcurrentG,
					goWhich, category);
			request.setAttribute("AboutUsList", AboutUsList);
			request.setAttribute("pageBar", AboutUsDaoIns.getDaoPage());
			AboutUsDaoIns.closed();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("/aboutus/aboutus.jsp");
		rd.forward(request, response);
	}

}
